﻿using aier.business.logic;
using aier.dataaccess.model;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;

namespace aier.wx.api.selfhost.Controllers
{
    /// <summary>
    /// 任务
    /// </summary>
    //[Authorize]
    [RoutePrefix("api/Task")]
    public class TaskController : ApiController
    {
        /// <summary>
        /// 日志
        /// </summary>
        public ILog log { get; set; }
        /// <summary>
        /// 任务逻辑
        /// </summary>
        public TaskBll taskBll { get; set; }
        /// <summary>
        /// 公共逻辑
        /// </summary>
        public CommonBll commonBll { get; set; }

        /// <summary>
        /// 工具类
        /// </summary>
        public ApiTool apiTool { get; set; }


        /// <summary>
        /// 获取首页信息
        /// </summary>
        /// <param name="ownerId">负责人</param>
        /// <param name="pageNum">当前页</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="order">排序方向asc/desc</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("GetTasknfoPagin")]
        public IHttpActionResult GetTasknfoPagin(int pageNum, int pageSize, string order)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            var ownerId = userInfo.userid;
            PageResult<TaskView> TaskInfos
                = taskBll.QueryTaskByOwnerIdPage(ownerId, pageNum, pageSize, order);
            return Ok<PageResult<TaskView>>(TaskInfos);
        }

        [HttpGet]
        [Authorize]
        [ActionName("GetTasknfoPagin1")]
        public IHttpActionResult GetTasknfoPagin1(int pageNum, int pageSize, string order)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            var ownerId = userInfo.userid;
            PageResult<TaskView> TaskInfos
                = taskBll.QueryTaskByOwnerIdPage1(ownerId, pageNum, pageSize, order);
            return Ok<PageResult<TaskView>>(TaskInfos);
        }

        /// <summary>
        /// 根据任务ID获取任务详情
        /// </summary>
        /// <param name="taskid"></param>
        [HttpGet]
        [ActionName("GetTaskDetail")]
        [Authorize]
        [ResponseType(typeof(TaskView))]

        public IHttpActionResult GetTaskDetail(Guid taskid)
        {
            return Ok(taskBll.QueryTaskInfoByTaskId(taskid));
        }


        /// <summary>
        /// 更新任务状态
        /// </summary>
        /// <param name="id">任务ID</param>
        /// <param name="stateCode">任务状态</param>
        /// <param name="taskName">任务实体名称</param>
        [HttpPost]
        [ActionName("UpdateTaskState")]
        public void UpdateTaskState(Guid id, int stateCode, string taskName)
        {
            commonBll.UpdateEntityState(id, stateCode, taskName);
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="ownId"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetTaskByOwnId")]
        public IHttpActionResult GetTaskByOwnId(string ownId)
        {
            var tasks = taskBll.QueryTaskByOwnerId(new Guid(ownId));
            return Ok(tasks);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="leadId"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetTaskByLeadId")]
        public IHttpActionResult GetTaskByLeadId(string leadId)
        {
            var tasks = taskBll.QueryTaskByLeadId(new Guid(leadId));
            return Ok(tasks);
        }


        /// <summary>
        /// 根据姓名或手机号码或座机搜索任务信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetTaskByNameOrPhineOrLandline")]
        [Authorize]
        public IHttpActionResult GetTaskByNameOrPhineOrLandline(string condition, int pageNum, int pageSize)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            var ownerId = userInfo.userid;
            var tasks = taskBll.QueryTaskInfoList(ownerId, condition, pageNum, pageSize);
            return Ok(tasks);
        }

        /// <summary>
        /// 根据任务Id获取筛查信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetScreenNoticeViewData")]
        [Authorize]
        public IHttpActionResult GetScreenNoticeViewData(Guid taskId)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            var ownerId = userInfo.userid;
            var tasks = taskBll.GetScreenNoticeViewData(taskId, ownerId);
            return Ok(tasks);
        }

        /// <summary>
        /// 根据Id及实体名称修改状态
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("UpdateState")]
        [Authorize]
        public IHttpActionResult UpdateState(Guid Id, string entityName, int state, int status)
        {
            taskBll.UpdateState(Id, entityName, state, status);
            return Ok();
        }
    }
}
